/**
  ******************************************************************************
  * @file    main.c
  * @author  MCU Application Team
  * @Version V1.0.0
  * @Date    2020-10-19
  * @brief   main function
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "mcu_peripheral.h"
#include "six_step_cmp.h"
#include "six_step_fsm.h"
#include "key.h"

UART_HandleTypeDef DebugUartHandle;

/**
  * @brief  writes a character to the usart
  * @param  ch
  *         *f
  * @retval the character
  */
int fputc(int ch, FILE *f)
{
  /* Send a byte to USART */
  HAL_UART_Transmit(&DebugUartHandle, (uint8_t *)&ch, 1, 1000);

  return (ch);
}
/**
  * @brief  DEBUG_USART GPIO Config,Mode Config,115200 8-N-1
  * @param  None
  * @retval None
  */
void DebugPortConfig(void)
{
	GPIO_InitTypeDef  	GPIO_InitStruct;

	__HAL_RCC_USART1_CLK_ENABLE();
	__HAL_RCC_GPIOF_CLK_ENABLE();

	DebugUartHandle.Instance          = USART1;

	DebugUartHandle.Init.BaudRate     = 2000000;
	DebugUartHandle.Init.WordLength   = UART_WORDLENGTH_8B;
	DebugUartHandle.Init.StopBits     = UART_STOPBITS_1;
	DebugUartHandle.Init.Parity       = UART_PARITY_NONE;
	DebugUartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
	DebugUartHandle.Init.Mode         = UART_MODE_TX_RX;

	HAL_UART_Init(&DebugUartHandle);
	#ifdef UART_DEBUG

	/**USART1 GPIO Configuration
	PF1     ------> USART1_TX-AF8
	*/
	GPIO_InitStruct.Pin = GPIO_PIN_1;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF8_USART1;
	HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
	
	
	printf("DebugPort Init\r\n");
	#endif
}


//******************************************************************************************************************************//	
/** @defgroup main 
  *  @{
    * @brief Upate main  
    * @retval None
*/
int main(void)
{		
	HAL_Init();
	SystemClock_Config();	
	
	HAL_Delay(200);
	
	DebugPortConfig();	
	
	MC_6StepCmp_IOInit();
	KeyInit();
	
	OCComp_Init();
	ZCPComp_Init();
	MC_6StepCmp_Init();
	MC_6StepCmp_ADC_Init(); 
	MC_6StepCmp_PWM_Init();
	
	MC_SixStep_Parameters.SystemInitFlag = 1;
	MC_SixStep_Parameters.MotorStartUp = 0;

	while (1)
	{
		
	}
}
void MC_6StepCmp_1msTimerUpdateIsr()
{
	if(MC_SixStep_Parameters.SystemInitFlag == 1)
	{
		//Acceleration control
		MC_6StepCmp_AccControl();
		MC_6StepCmp_RunStopCtrl();
		MC_6StepCmp_ErrScan();
		MC_6StepCmp_VdcProc();
		
		KeyTask();
		if(UserKey.KeyCmd == KEY_EVEN_PRESS)
		{
			UserKey.KeyCmd = KEY_EVEN_NULL;
			(MC_SixStep_Parameters.MotorStartUp == 1) ? (MC_SixStep_Parameters.MotorStartUp=0) : (MC_SixStep_Parameters.MotorStartUp = 1);
		}
		
//		MC_SixStep_Parameters.MotorStartUp=1;
	}
}

//******************************************************************************************************************************//
/** @defgroup Error_Handler
  *  @{
    * @brief Error_Handler
    * @retval None
*/
void Error_Handler(void)
{
	#ifdef UART_DEBUG
	printf("Error_Handler\r\n");
	#endif
	MC_SixStep_Parameters.MotorErrCode = MOTOR_ELSE_ERROR;	
  	MC_6StepCmp_ErrHandle();
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif /* USE_FULL_ASSERT */


/* Private function -------------------------------------------------------*/



